/* ******************************* *
 *       -=# Array Life #=-        *
 *             =====               *
 *         By: TarpeyD12           *
 * =============================== *
 *        Version: 0.3.5.9         *
 * ******************************* *
 *     Logging Functions file.     *
 *        alfunc_logging.c         *
 * ******************************* */

#include "array_life.h"

/* The loggong functions. */
int log_occp_grid(FILE *file);
int log_stats(FILE *file, int stats_x, int stats_y, int dna_size);
int log_all_stats(FILE *file, int gs_x, int gs_y, int dna_size);
void log_genstats(FILE *file);
int log_plate(FILE *file);
int get_plate(FILE *file);

/* the arrays and grids. */
extern int grid_box[GRIDSIZEX][GRIDSIZEY][DNASIZE];
extern int occp[GRIDSIZEX][GRIDSIZEY];

/* the used file pointers. */
extern FILE *log_file;
extern FILE *usr_log_file;
extern FILE *plate;

extern char p_str[512];

/* what loop it is. */
extern int loop;

/* The loggong functions. */

int log_occp_grid(FILE *file) {
	int x, y;
	/*file=fopen("log.al.txt", "a+");*/
	if(!file){
		printw("LogFile ERROR.\n");
		refresh(); endwin();
		exit (0); /*return 0;*/   
	}
	fprintf(file, "\n\n  ");
	for(x=1;(GRIDSIZEX-1)>x;x++) {fprintf(file, " %2d", x);}
	fprintf(file, "\n");
	/*fprintf(log_file, "\n\n");*/
	for(y=1;(GRIDSIZEY-1)>y;y++) {
		fprintf(file, " %2d", y);
		for(x=1;(GRIDSIZEX-1)>x;x++) {
			switch(occp[x][y]){
				case 0: fprintf(file, " - "); break;
				case 2: fprintf(file, " W "); break;
				case 1:
				case 3: fprintf(file, " # "); break;
				default: fprintf(file, " %d ", occp[x][y]); break;
				
			}
			/*fprintf(log_file, "  ");*/
		}
		fprintf(file, "\n\n");
	}
	fclose(file);
	return 1;
};

int log_stats(FILE *file, int stats_x, int stats_y, int dna_size) {
	int dna_tmp=0;
	/*file=fopen("log.al.txt", "a+");*/
	if(!file){
		printw("LogFile ERROR.\n");
		refresh(); endwin();
		exit (0); /*return 0;*/
	}
	fprintf(file, "\nthe statistics for: grid_box[%d][%d]:\n", stats_x, stats_y);
	fprintf(file, "occupied: ");
	switch(occp[stats_x][stats_y]) { 
		case 3:
		case 1: fprintf(file, "Yes\n"); break; 
		case 0: fprintf(file, "No\n"); break;
	};
	fprintf(file, "DNA: ");
	for(dna_tmp=0;dna_tmp<dna_size;dna_tmp++){
		switch(grid_box[stats_x][stats_y][dna_tmp]){
			case 0: fprintf(file, "-"); break;
			case 1: fprintf(file, "#"); break;
			/*printf("%d", grid_box[stats_x][stats_y][dna_tmp]);*/
		}
	}
	fprintf(file, "\nAAS: ");
	for(dna_tmp=0;dna_tmp<dna_size;dna_tmp++){
		if(grid_box[stats_x][stats_y][dna_tmp]==0 && grid_box[stats_x][stats_y][(dna_tmp+1)]==0 && grid_box[stats_x][stats_y][(dna_tmp+2)]==0)
			{fprintf(file, "[0]");}/* 000 */
		else if(grid_box[stats_x][stats_y][dna_tmp]==0 && grid_box[stats_x][stats_y][(dna_tmp+1)]==0 && grid_box[stats_x][stats_y][(dna_tmp+2)]==1)
			{fprintf(file, "[1]");}/* 001 */
		else if(grid_box[stats_x][stats_y][dna_tmp]==0 && grid_box[stats_x][stats_y][(dna_tmp+1)]==1 && grid_box[stats_x][stats_y][(dna_tmp+2)]==0)
			{fprintf(file, "[2]");}/* 010 */
		else if(grid_box[stats_x][stats_y][dna_tmp]==1 && grid_box[stats_x][stats_y][(dna_tmp+1)]==0 && grid_box[stats_x][stats_y][(dna_tmp+2)]==0)
			{fprintf(file, "[3]");}/* 100 */
		else if(grid_box[stats_x][stats_y][dna_tmp]==0 && grid_box[stats_x][stats_y][(dna_tmp+1)]==1 && grid_box[stats_x][stats_y][(dna_tmp+2)]==1)
			{fprintf(file, "[4]");}/* 011 */
		else if(grid_box[stats_x][stats_y][dna_tmp]==1 && grid_box[stats_x][stats_y][(dna_tmp+1)]==0 && grid_box[stats_x][stats_y][(dna_tmp+2)]==1)
			{fprintf(file, "[5]");}/* 101 */
		else if(grid_box[stats_x][stats_y][dna_tmp]==1 && grid_box[stats_x][stats_y][(dna_tmp+1)]==1 && grid_box[stats_x][stats_y][(dna_tmp+2)]==0)
			{fprintf(file, "[6]");}/* 110 */
		else if(grid_box[stats_x][stats_y][dna_tmp]==1 && grid_box[stats_x][stats_y][(dna_tmp+1)]==1 && grid_box[stats_x][stats_y][(dna_tmp+2)]==1)
			{fprintf(file, "[7]");}/* 111 */
		
		dna_tmp++;
		dna_tmp++;
	}
	fprintf(file, "\nStrength: %d", strength(stats_x, stats_y, dna_size));
	fprintf(file, "\n\n");
	/*fclose(file);*/
	return 1;
};

int log_all_stats(FILE *file, int gs_x, int gs_y, int dna_size) {
	int x, y, cnt=0;
	/*file=fopen("log.al.txt", "a+");*/
	if(!file){
		printf("LogFile ERROR.\n");
		exit (0); /*return 0;*/   
	}
	chk_grid(); /* requires the chk_grid(); to be functioning */
	for(y=0;y<gs_y;y++){
		for(x=0;x<gs_x;x++){
			if(occp[x][y]==1){
				log_stats(file, x, y, dna_size); /* requires the log_stats(); to be functioning */
				cnt++;
			}
		}
	}
	if(cnt==0){fprintf(file, "\nno living cells.\n");}
	fclose(file);
	return(cnt);
};



void log_genstats(FILE *file) {
	/*file=fopen("log.al.txt", "a+");*/
	if(!file){
		printf("UsrLogFile ERROR.\n");
		exit (0); /*return 0;*/   
	}
	fprintf(file, "Generation: %-10d Strongest: %-5d Weakest: %-5d Cells: %-5d\n", loop,  strongest(), weakest(), cell_cnt(GRIDSIZEX, GRIDSIZEY, DNASIZE));
};


int log_plate(FILE *file) {/* writes the created grid to file */
	int x=0, y=0;
	if(!file){
		printf("PlateFile internal ERROR #1.\n");
		exit (0); /*return 0;*/   
	}
	for(y=0;y<GRIDSIZEY;y++){
		for(x=0;x<GRIDSIZEX;x++){
			fprintf(file, " %d", occp[x][y]);
		}
		fprintf(file, "\n");
	}
	return 1;
};

int get_plate(FILE *file) {
	int x=0, y=0;
	unsigned int tmp=0;
	if(!file){
		printf("PlateFile internal ERROR #2.\n");
		exit (0); /*return 0;*/   
	}
	/*gridsize(GRIDSTUFF);*/
	for(y=0;y<GRIDSIZEY;y++){
		for(x=0;x<GRIDSIZEX;x++){
			fscanf(file, "%d", &tmp);
			switch(tmp){
				case DEAD_CELL:
					occp[x][y]==DEAD_CELL;
				break;
				
				case LIVE_CELL:
					create(x, y, DNASIZE);
				break;
				
				case WALL_CELL:
					wall(x, y);
				break;
				
				case TEMP_CELL:
					occp[x][y]==TEMP_CELL;
				break;
				
				default:
					occp[x][y]==tmp;
					/*printf("PlateFile ERROR #3.\n");
					exit(0);*/
				break;
			}
		}
	}
	return 1;
};
